import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:winkai/entity/chat_msg.dart';
import 'package:winkai/entity/subscribe_from.dart';
import 'package:winkai/helper/iap/iap_helper.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_align_widget.dart';
import 'package:winkai/utils/gaussian_blur_utils.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:winkai/utils/net_image_load_utils.dart';
import 'package:winkai/utils/page_jump_utils.dart';

class ChatMsgImageWidget extends StatelessWidget {
  const ChatMsgImageWidget({
    super.key,
    required this.message,
    this.onAvatarClick,
  });

  final ChatMsg message;
  final void Function()? onAvatarClick;

  @override
  Widget build(BuildContext context) {
    var isSend = message.isSend;
    return ChatMsgAlignWidget(
      isSend: isSend,
      children: [
        if (!isSend)
          GestureDetector(
            onTap: () {
              onAvatarClick?.call();
            },
            child: NetImageLoadUtils.loadCircle(
                message.messageInfo.user?.avatar, 40.w),
          ),
        if (!isSend) SizedBox(width: 8.w),
        _buildImageWidget(context)
      ],
    );
  }

  Widget _buildImageWidget(BuildContext context) {
    var imageUrl = message.messageInfo.msg.content;
    var isSend = message.isSend;
    var isPrivateChat = message.messageInfo.msg.isPrivateChat;
    var imageWidth = 240.w;
    var imageHeight = 240.w;
    var imageWidget = NetImageLoadUtils.load(imageUrl, imageWidth, imageHeight,
        isSetRadius: true, radius: 15);
    var isHide = !isSend && !IAPHelper().isVip && isPrivateChat;
    return isHide
        ? GestureDetector(
            onTap: () {
              PageJumpUtils.toVipPage(SubscribeFrom.lockPic);
              // reportEvent('c_news_lockpic');
            },
            child: Container(
              width: imageWidth,
              height: imageHeight,
              decoration:
                  BoxDecoration(borderRadius: BorderRadius.circular(16)),
              clipBehavior: Clip.antiAlias,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  imageWidget,
                  GaussianBlurUtils.blur(color: const Color(0x4d000000)),
                  ImageUtils.getImage('ic_ai_unlock1', 40.w, 40.w),
                ],
              ),
            ),
          )
        : GestureDetector(
            onTap: () {
              // showChatImageDialog(context, imageUrl: imageUrl);
            },
            child: imageWidget);
  }
}
